package net.qhzw.vip.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.qhzw.vip.qiyu.dto.DeliveryManDto;
import net.qhzw.vip.qiyu.entity.Area;
import net.qhzw.vip.qiyu.entity.DeliveryMan;
import net.qhzw.vip.qiyu.entity.Shop;
import net.qhzw.vip.qiyu.feign.ShopFeignClient;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.mapper.AreaMapper;
import net.qhzw.vip.qiyu.mapper.DeliveryManMapper;
import net.qhzw.vip.qiyu.service.AreaService;
import net.qhzw.vip.qiyu.service.DeliveryManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DeliveryManServiceImpl extends ServiceImpl<DeliveryManMapper, DeliveryMan>
        implements DeliveryManService {


    @Autowired
    private DeliveryManMapper deliveryManMapper;

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private ShopFeignClient shopFeignClient;

    @Autowired
    private AreaService areaService;

    @Override
    public DeliveryMan info(Long id) {
        DeliveryMan deliveryMan = deliveryManMapper.selectById(id);
        if (deliveryMan != null) {
            Area area = areaMapper.selectAreaById(deliveryMan.getAreaId());
            deliveryMan.setResponsibleArea(area.getAddre() + "-" + deliveryMan.getResponsibleArea() + "-" + deliveryMan.getRealName());
        }
        return deliveryMan;
    }

    @Override
    public DeliveryMan getByAreaName(String pickupPointName) {
        return deliveryManMapper.selectByAreaName(pickupPointName);
    }

    @Override
    public List<DeliveryManDto> selectDeliveryManDto(List<Long> areaIds) {
        List<DeliveryManDto> deliveryManDtos = deliveryManMapper.selectDeliveryManDto(areaIds);
        return deliveryManDtos;
    }

    @Override
    public List<DeliveryMan> queryDetailedAddrByIds(List<Long> ids) {
        List<DeliveryMan> list = deliveryManMapper.selectDetailedAddrByIds(ids);
        if (!CollectionUtils.isEmpty(list)) {
            for (DeliveryMan deliveryMan : list) {
                deliveryMan.setAddre(deliveryMan.getAddre() + "-" + deliveryMan.getRealName());
            }
        }
        return list;
    }


    /**
     * 商户绑定自提点
     *
     * @param deliveryMan
     * @return
     */
    @Override
    public boolean bindMerchants(DeliveryMan deliveryMan) {
        Long shopId = deliveryMan.getShopId();
        Shop data = shopFeignClient.findById(shopId).getData();
        if (data == null) {
            throw new ApiException("商户信息有误!");
        }
        if (data.getShopType() == null || data.getShopType() != 5) {
            throw new ApiException(data.getShopName() + " 商户不支持!");
        }
        deliveryManMapper.reset(shopId);
        deliveryMan.setStatus((byte) 1);
        return updateById(deliveryMan);
    }



    @Override
    public List<DeliveryMan> pickupPoint(Integer isVip) {
        List<DeliveryMan> list = list(new LambdaQueryWrapper<DeliveryMan>()
                .eq(DeliveryMan::getStatus, 1)
                .eq(isVip != null, DeliveryMan::getIsVip, isVip));


        if (!CollectionUtils.isEmpty(list)) {
            //stream List<object> -> List<Long>
            List<Long> areaIds = list.stream().map(DeliveryMan::getAreaId).collect(Collectors.toList());




        }

        return list;
    }
}

